(function(global) {
	//  获取Vue等
	var Vue = global.Vue,
		mui = global.mui,
		$ = global.$ = global.mui,
		locale = global.locale,
		lang = global.dataManager.getLang(),
		VueI18n = global.VueI18n,
		dataManager = global.dataManager,
		i18n = new VueI18n({
			locale: lang,
			messages: locale
		});

	//  加入国际化配置
	Vue.mixin({
		i18n: i18n,
		methods: {
			setLang: function(lang) {
				this.$i18n.locale = lang;
				global.dataManager.setLang(lang);
			},
			getLang: function(lang) {
				return i18n.getLocaleMessage(lang);
			}
		}
	});

	//	不打印warn日志
	global.console.warn = mui.noop;

	/**
	 * 初始化所有的库
	 * @type {{echarts: {js: [*]}, picker: {js: [*], css: [*]}, sceneHeader: {js: [*]}}}
	 */
	var libs = {
		echarts: {
			js: ['js/libs/echarts.min.js', 'js/element/echarts.js']
		},
		picker: {
			js: ['js/libs/mui.picker.min.js', 'js/libs/mui.poppicker.js'],
			css: ['css/mui.picker.min.css', 'css/mui.poppicker.css']
		},
		sceneHeader: {
			js: ['js/element/scene-header.js']
		}
	};

	libs.datePicker = {
		js: libs.picker.js.concat(['js/libs/mui.dtpicker.js']),
		css: libs.picker.css.concat(['css/mui.dtpicker.css'])
	};

	/**
	 * 加载js文件
	 * @param name
	 * @param callback
	 */
	function addJScript(name, callback) {
		var sc = document.createElement('script');
		sc.type = "text/javascript";
		sc.src = name;
		sc.async = false;
		sc.addEventListener('load', callback || $.noop, false);
		sc.addEventListener('error', function() {
			console.log('[DEBUG] ' + name + '加载失败。');
			callback && callback();
		}, false);
		document.querySelector('head').appendChild(sc);
	}

	/**
	 * 加载css文件
	 * @param name
	 * @param callback
	 */
	function addCss(name, callback) {
		var link = document.createElement('link');
		link.rel = "stylesheet";
		link.href = name;
		link.addEventListener('load', callback || $.noop, false);
		link.addEventListener('error', function() {
			console.log('[DEBUG] ' + name + '加载失败。');
		}, false);
		document.querySelector('head').appendChild(link);
	}

	/**
	 * 获取文件网页根目录
	 * @returns {*}
	 */
	function getRoot() {
		return window.location.href.split('dist/')[0];
	}

	/**
	 * 加载组件库
	 * @param {Object} libName
	 * @param callback 加载完成的回调函数
	 */
	function loadLib(libName, callback) {
		var libNames = Array.isArray(libName) ? libName : [libName];
		var fileCount = 0;
		var loadedFileCount = 0;

		libNames.forEach(function(name) {
			var lib = libs[name];
			if(lib) {
				fileCount += (lib.js || []).concat(lib.css || []).length;
			} else {
				throw new Error('没有找到名为的"' + libName + '"库，目前支持的库：' + Object.keys(libs).join(','));
			}
		});

		/**
		 * 加载完成才会处理回调函数
		 */
		function loaded() {
			loadedFileCount++;
			if(loadedFileCount === fileCount) {
				callback && callback();
			}
		}

		/**
		 * 加载所有的库
		 */
		libNames.forEach(function(name) {
			var lib = libs[name];
			if(lib) {
				// 先加载js文件,再加载css文件
				(lib.js || []).forEach(function(s) {
					addJScript(getRoot() + s, loaded);
				});
				(lib.css || []).forEach(function(c) {
					addCss(getRoot() + c, loaded);
				});
			} else {
				throw new Error('没有找到名为的"' + libName + '"库，目前支持的库：' + Object.keys(libs).join(','));
			}
		});
	}

	/**
	 * @description 填充0
	 * @param v
	 * @returns {string}
	 */
	function fillStr(v) {
		return v < 10 ? '0' + v : v;
	}

	/**
	 * 格式化时间
	 * @param d  时间
	 * @param patten  格式文本   y 年；M 月；d 日；H 小时；m 分钟；s 秒；
	 * @returns {string}  格式化以后的实践
	 */
	function formatDate(d, patten) {
		patten = patten || 'y-M-d H:m:s';
		var date = d ? new Date(d) : new Date();
		return patten.replace(/y/g, date.getFullYear())
			.replace(/M/g, fillStr(date.getMonth() + 1))
			.replace(/d/g, fillStr(date.getDate()))
			.replace(/H/g, fillStr(date.getHours()))
			.replace(/m/g, fillStr(date.getMinutes()))
			.replace(/s/g, fillStr(date.getSeconds()));
	}

	/**
	 * 获取最近一周的时间
	 * @param includeToday default false
	 */
	function nearOneWeek(includeToday) {
		var now = +new Date();
		var arr = includeToday ? [6, 5, 4, 3, 2, 1, 0] : [7, 6, 5, 4, 3, 2, 1];
		return arr.map(function(p1) {
			return formatDate(now - (p1 * 24 * 60 * 60 * 1000), 'M-d');
		});
	}

	$.loadLib = loadLib;

	$.util = {
		formatDate: formatDate,
		nearOneWeek: nearOneWeek,
		getDevSN: dataManager.get('sn'),
		setDevSN: dataManager.set('sn'),
		setShopId: dataManager.set('shopId'),
		getShopId: dataManager.get('shopId'),
		setShopInfo: dataManager.set('shopInfo'),
		getShopInfo: dataManager.get('shopInfo', true)
	};

	$.plusReady(function() {
		console.log('当前有' + plus.webview.all().length + '个webview <= ' + location.href.split('/').pop());
	});
})(window);